package com.szxy.miaosha.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


/**
 * @Auther:wzer
 * @Date:2020/9/6
 * @Description:com.szxy.miaosha.config
 * @Version:1.0
 **/
@Service
public class RedisFactory {

    @Autowired
    private RedisConfig redisConfig;

    @Bean
    public JedisPool getRedisFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);

        //Exception SocketTimeout： Connect Timeout
        //  Timeout 配置时间过短， 导致程序出现 SocketTimeOut
        JedisPool jedisPool = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort()
                , redisConfig.getTimeout() * 1000, redisConfig.getPassword(), 0);

        return jedisPool;
    }

}
